﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SharpShare.Diagnostics {
    public static class LogWriter {
        private static object _consoleWriteLock = new object();

        public static void WriteHtml(this LogEntry entry, StreamWriter writer) {
            entry.Style((text, color) => {
                string colorName;

                switch (color) {
                    case ConsoleColor.DarkBlue:
                        colorName = "rgb(0, 0, 128)";
                        break;
                    case ConsoleColor.DarkGreen:
                        colorName = "rgb(0, 128, 0)";
                        break;
                    case ConsoleColor.DarkRed:
                        colorName = "rgb(128, 0, 0)";
                        break;
                    default:
                        colorName = color.ToString().ToLower();
                        break;
                }

                string writeText = text.Replace(" ", "&nbsp;").Replace("\r\n", "<br/>");
                writer.Write("<span style=\"color: {0}\">{1}</span>", colorName, writeText);
            });
        }

        public static void Style(this LogEntry entry, Action<string, ConsoleColor> writer) {
            ConsoleColor color = ConsoleColor.Black;

            string type = null;
            if (entry.Type == EntryType.Information)
                type = "INFO";
            else
                type = entry.Type.ToString().ToUpper();

            switch (entry.Type) {
                case EntryType.Information:
                    color = ConsoleColor.DarkGreen;
                    break;
                case EntryType.Debug:
                    color = ConsoleColor.DarkBlue;
                    break;
                case EntryType.Warning:
                    color = ConsoleColor.Red;
                    break;
                case EntryType.Error:
                    color = ConsoleColor.DarkRed;
                    break;
            }

            string sender = "Unknown";
            if (entry.Sender != null) {
                if (entry.Sender is ILogProvider)
                    sender = (entry.Sender as ILogProvider).Name;
                else
                    sender = entry.Sender.ToString();
            }

            if (sender.Length > 15)
                sender = sender.Substring(0, 15);

            writer(string.Format("{0,-16}", sender), ConsoleColor.Black);
            writer(string.Format("{0,8}  ", type), color);
            writer(entry.Message, ConsoleColor.Black);
            writer("\r\n", ConsoleColor.Black);

            if (entry.Exception != null) {
                writer("\r\n", ConsoleColor.Black);
                writer(entry.Exception.ToString(), ConsoleColor.Black);
                writer("\r\n", ConsoleColor.Black);
            }

            if (entry.DebugObject != null) {
                writer("\r\n", ConsoleColor.DarkBlue);
                writer(entry.DebugObject.ToString(), ConsoleColor.DarkBlue);
                writer("\r\n", ConsoleColor.DarkBlue);
            }
        }

        public static void WriteToConsole(this LogEntry entry) {
            lock (_consoleWriteLock) {
                entry.Style((text, color) => {
                    Console.ForegroundColor = color;
                    Console.Write(text);
                });
            }
        }
    }
}
